package com.photoeditor.demo.ui.widget.image.edit;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.android.gpuimage.AdjustGPUImageView;
import com.android.gpuimage.GPUImage;
import com.android.gpuimage.GPUImageFilter;
import com.common.base.utils.thread.ThreadPool;
import com.photoeditor.R;
import com.photoeditor.demo.ui.activity.edit.EditorImageActivity;
import com.photoeditor.demo.ui.widget.blur.LinearBlurTipView;
import com.photoeditor.demo.ui.widget.blur.RadialBlurTipView;
import com.photoeditor.demo.ui.widget.image.CustomTabCheckBg;
import com.photoeditor.demo.ui.widget.image.IOnProgressChange;


/**
 * 编辑界面编辑TileShift的View
 */
public class TileShiftBarView extends LinearLayout implements OnClickListener, IDealInit, IOnProgressChange, GPUImage.OnBlurEventListener {

    private AdjustGPUImageView mAdjustGPUImageView;
    private RadialBlurTipView mRadialBlurTipView;
    private LinearBlurTipView mLinearBlurTipView;

    private CustomTabCheckBg mTiltShiftRadial;
    private CustomTabCheckBg mTiltShiftLinear;

    private boolean mIsTiltShiftRadial = true;

    private GPUImageFilter mDefaultFilter;

    private EditorImageActivity mActivity;

    private int mTiltShiftRadialProgress = 50;
    private int mTiltShiftLinearProgress = 50;

    private long mClickBtnTime = 0;

    public TileShiftBarView(Context context) {
        this(context, null);
    }

    public TileShiftBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mActivity = (EditorImageActivity) context;
    }

    public void setmAdjustGPUImageView(AdjustGPUImageView mAdjustGPUImageView) {
        this.mAdjustGPUImageView = mAdjustGPUImageView;
        mAdjustGPUImageView.getGPUImage().setOnBlurEventListener(this);
    }

    public void setTipView(RadialBlurTipView radialBlurTipView, LinearBlurTipView linearBlurTipView) {
        this.mRadialBlurTipView = radialBlurTipView;
        this.mLinearBlurTipView = linearBlurTipView;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    @Override
    public void init() {
        mDefaultFilter = new GPUImageFilter();
        mTiltShiftRadial = (CustomTabCheckBg) findViewById(R.id.tiltshift_radial);
        mTiltShiftLinear = (CustomTabCheckBg) findViewById(R.id.tiltshift_linear);

        mTiltShiftRadial.setOnClickListener(this);
        mTiltShiftLinear.setOnClickListener(this);

        mIsTiltShiftRadial = true;
        mAdjustGPUImageView.setSelectiveBlurEnable(true);
        mAdjustGPUImageView.setTiltShiftEnable(false);
        if (mIsTiltShiftRadial) {
            mActivity.showInsideBottomBarWithProgress(mTiltShiftRadialProgress);
        } else {
            mActivity.showInsideBottomBarWithProgress(mTiltShiftLinearProgress);
        }
        initView();
        updateTiltShiftButton();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        mClickBtnTime = System.currentTimeMillis();
        if (id == R.id.tiltshift_radial) {
            mIsTiltShiftRadial = true;
            if (!mAdjustGPUImageView.isSelectiveBlurEnable()) {
                mAdjustGPUImageView.setTiltShiftEnable(false);
                mAdjustGPUImageView.setSelectiveBlurEnable(true);
            }
            updateTiltShiftButton();
            mActivity.showInsideBottomBarWithProgress(mTiltShiftRadialProgress);
            mAdjustGPUImageView.setSelectiveBlurSize(range(mTiltShiftRadialProgress, 1.0f, 3f));
            mAdjustGPUImageView.requestRender();

        } else if (id == R.id.tiltshift_linear) {
            mIsTiltShiftRadial = false;
            if (!mAdjustGPUImageView.isTiltShiftEnable()) {
                mAdjustGPUImageView.setSelectiveBlurEnable(false);
                mAdjustGPUImageView.setTiltShiftEnable(true);
            }
            updateTiltShiftButton();
            mActivity.showInsideBottomBarWithProgress(mTiltShiftLinearProgress);
            mAdjustGPUImageView.setTiltShiftBlurSize(range(mTiltShiftLinearProgress, 1.0f, 3f));
            mAdjustGPUImageView.requestRender();
        }
    }

    private void updateTiltShiftButton() {
        if (mAdjustGPUImageView.isSelectiveBlurEnable()) {
            mTiltShiftRadial.setChecked(true);
        } else {
            mTiltShiftRadial.setChecked(false);
        }
        if (mAdjustGPUImageView.isTiltShiftEnable()) {
            mTiltShiftLinear.setChecked(true);
        } else {
            mTiltShiftLinear.setChecked(false);
        }
    }

    /**
     * 复原原来的状态
     */
    public void cancel() {
        mTiltShiftRadialProgress = 50;
        mTiltShiftLinearProgress = 50;
        mAdjustGPUImageView.setSelectiveBlurSize(range(mTiltShiftRadialProgress, 1.0f, 3f));
        mAdjustGPUImageView.setTiltShiftBlurSize(range(mTiltShiftLinearProgress, 1.0f, 3f));
        if (mIsTiltShiftRadial) {
            mActivity.showInsideBottomBarWithProgress(mTiltShiftRadialProgress);
        } else {
            mActivity.showInsideBottomBarWithProgress(mTiltShiftLinearProgress);
        }
        mAdjustGPUImageView.setSelectiveBlurEnable(false);
        mAdjustGPUImageView.setTiltShiftEnable(false);
        updateTiltShiftButton();
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == View.VISIBLE) {
            mRadialBlurTipView.setVisibility(VISIBLE);
            if (mAdjustGPUImageView != null) {
                mAdjustGPUImageView.setFilter(mDefaultFilter);
            }
            if (mIsTiltShiftRadial) {
                mAdjustGPUImageView.setSelectiveBlurEnable(true);
                mAdjustGPUImageView.setTiltShiftEnable(false);
                updateTiltShiftButton();
                mActivity.showInsideBottomBarWithProgress(mTiltShiftRadialProgress);
                mAdjustGPUImageView.setSelectiveBlurSize(range(mTiltShiftRadialProgress, 1.0f, 3f));
                mAdjustGPUImageView.requestRender();
            } else {
                mAdjustGPUImageView.setSelectiveBlurEnable(false);
                mAdjustGPUImageView.setTiltShiftEnable(true);
                updateTiltShiftButton();
                mActivity.showInsideBottomBarWithProgress(mTiltShiftLinearProgress);
                mAdjustGPUImageView.setTiltShiftBlurSize(range(mTiltShiftLinearProgress, 1.0f, 3f));
                mAdjustGPUImageView.requestRender();
            }
        } else {
            mRadialBlurTipView.setVisibility(INVISIBLE);
            mLinearBlurTipView.setVisibility(INVISIBLE);
        }
    }

    private void initView() {
        setBackgroundResource(R.drawable.image_edit_sencond_bg);
    }

    @Override
    public void onProgressChange(int progress) {
        if (mIsTiltShiftRadial) {
            mTiltShiftRadialProgress = progress;
            mAdjustGPUImageView.setSelectiveBlurSize(range(mTiltShiftRadialProgress, 1.0f, 3f));
            mAdjustGPUImageView.requestRender();
        } else {
            mTiltShiftLinearProgress = progress;
            mAdjustGPUImageView.setTiltShiftBlurSize(range(mTiltShiftLinearProgress, 1.0f, 3f));
            mAdjustGPUImageView.requestRender();
        }
    }

    protected float range(final int percentage, final float start, final float end) {
        return (end - start) * percentage / 100.0f + start;
    }

    @Override
    public void onShowLinearBlurEffect(float angle, float focusX, float focusY, float width) {
        mRadialBlurTipView.setVisibility(View.INVISIBLE);
        mLinearBlurTipView.setVisibility(View.VISIBLE);
        mLinearBlurTipView.onShowLinearBlurEffect(angle, focusX, focusY, width);
    }

    @Override
    public void onShowBlurEffect(final float radiu, final float focusX, final float focusY) {
        ThreadPool.runUITask(new Runnable() {
            @Override
            public void run() {
                mRadialBlurTipView.setVisibility(View.VISIBLE);
                mLinearBlurTipView.setVisibility(View.INVISIBLE);
                mRadialBlurTipView.onShowBlurEffect(radiu, focusX, focusY);
            }
        });
    }

    @Override
    public void onDismissBlurEffect() {
        ThreadPool.runUITask(new Runnable() {
            @Override
            public void run() {
                if (System.currentTimeMillis() - mClickBtnTime < 100) {
                    if (!mAdjustGPUImageView.isTiltShiftEnable()) {
                        mLinearBlurTipView.setVisibility(View.INVISIBLE);
                    } else {
                        mRadialBlurTipView.setVisibility(View.INVISIBLE);
                    }
                } else {
                    mLinearBlurTipView.setVisibility(View.INVISIBLE);
                    mRadialBlurTipView.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    @Override
    public void updateBlurExcludeCircleRadiu(final float radiu, final float focusX, final float focusY) {
        ThreadPool.runUITask(new Runnable() {
            @Override
            public void run() {
                mRadialBlurTipView.updateBlurExcludeCircleRadiu(radiu, focusX, focusY);
            }
        });
    }

    @Override
    public void updateLinearBlurExclude(final float angle, final float focusX, final float focusY, final float width) {
        ThreadPool.runUITask(new Runnable() {
            @Override
            public void run() {
                mLinearBlurTipView.updateLinearBlurExclude(angle, focusX, focusY, width);
            }
        });
    }
}
